package com.jmu.bishe.utils;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

/**
 *<pre><b><font color="blue">BeanUtils</font></b></pre>
 *
 *<pre><b>&nbsp;--继承扩展apache的 BeanUtils类--</b></pre>
 * <pre></pre>
 * JDK版本：JDK1.5.0
 * @author  <b>ulyn</b> 
 */
public class BeanUtils extends org.apache.commons.beanutils.BeanUtils {
	
	private static Logger logger = Logger.getLogger(BeanUtils.class);

	/**将HttpServletRequest解析并注入到Bean里面的辅助类。
	 * @param request
	 * @param obj
	 * @author: ulyn
	 */
	public static void RequestToBean(HttpServletRequest request, Object obj) {
		try {
			Map map = request.getParameterMap();
			populate(obj, map);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	/**将map注入到Bean里面的辅助类。
	 * @param map
	 * @param obj
	 * @author: ulyn
	 */
	public static void MapToBean(Map map, Object obj) {
		try {
			populate(obj, map);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	/**将requestobj非空属性的值注入到Bean里面的辅助类。
	 * @param requestobj
	 * @param obj
	 * @author: ulyn
	 */
	public static void BeanToBean(Object requestobj, Object obj) {
		try {
			Map map = describe(requestobj);
			for(Object key:map.keySet()){
				if(map.get(key) != null){
					setProperty(obj,(String)key,map.get(key));
				}
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			logger.info("BeanToBean转换错误");
			e.printStackTrace();
		}
	}
}
